package com.jie.deletefile.Calendar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jie.deletefile.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateActivity extends AppCompatActivity {

    private String TAG = "TAG";
    private android.widget.TextView tvdate;
    private android.widget.RelativeLayout activitydata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        this.activitydata = (RelativeLayout) findViewById(R.id.activity_data);
        this.tvdate = (TextView) findViewById(R.id.tv_date);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    final String strTime = getStrTime(getLongTime());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvdate.setText(strTime);
                        }
                    });

                    //每一秒更新一次时间
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();
    }

    //通过calendar类获取到当前时间
    private long getLongTime() {
        Calendar calendar = Calendar.getInstance();
        long timeInMillis = calendar.getTimeInMillis();
        Log.e(TAG, "getLongTime: "+timeInMillis );
        return timeInMillis;
    }


    /**
     * 将毫秒转为字符串方式的时间格式 (yyyy-MM-dd hh:mm:ss)
     * <p/>
     * *    * @param filetime   * @return
     */
    private String getStrTime(long filetime) {
        if (filetime == 0) {
            return "未知";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = dateFormat.format(new Date(filetime));
        return format;

    }


}
